package com.hmdp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.service.IBlogService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/blog")
@Api(tags = "探店博文接口")
public class BlogController {

    @Resource
    private IBlogService blogService;
    @Resource
    private IUserService userService;

    // 探店博文保存
    @ApiOperation("保存探店博文")
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {

        return blogService.saveBlog(blog);
    }
    // 探店博文点赞
    @ApiOperation("点赞探店博文")
    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        return blogService.likeBLog(id);
    }

    //显示点赞排行榜按照时间先后顺序
    @ApiOperation("显示点赞排行榜")
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id) {
        return blogService.queryBlogLikes(id);
    }
    // 探店自己的博文
    @ApiOperation("查询我的探店博文")
    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }
    // 探店热门博文
    @ApiOperation("查询热门探店博文")
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {

        return blogService.queryHotBlog(current);
    }
    //查看探店笔记
    @GetMapping("/{id}")
    public Result queryBlog(@PathVariable("id") Long id) {
        return blogService.queryBlogById(id);
    }

    //根据id查询博主的探店笔记
    @GetMapping("/of/user")
    public Result queryUserBlog(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                @RequestParam(value = "id") Long id){
        Page<Blog> blogPage = blogService.query().eq("user_id", id).
                page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));//new Page<>参数为当前页和每页显示的数量

        List<Blog> records = blogPage.getRecords();
        return Result.ok(records);
    }

    //好友关注-实现分页查询收邮箱
    @GetMapping("/of/follow")
    public Result queryBlogofFollow(
            @RequestParam(value = "lastId") Long max,
            @RequestParam(value = "offset", defaultValue = "0") Integer offset){
        return blogService.queryBlogofFollow(max, offset);
    }
}
